package com.sqs.math;

import java.util.List;

public class IntMatrix extends AbstractMatrix<IntMatrix> {

	private int[][] data;

	private IntMatrix() {
	}

	@Override
	public IntMatrix add(IntMatrix augend) {
		if (this.rows != augend.rows || this.cols != augend.cols) {
			throw new IllegalArgumentException("rows and cols must be equal when add");
		}
		
		IntMatrix result = new IntMatrix();
		
		result.rows = this.rows;
		result.cols = this.cols;

		result.data = new int[result.rows][result.cols];
		
		for(int i=0;i<result.rows;i++){
			for(int j=0;j<result.cols;j++){
				result.data[i][j] = this.data[i][j] + augend.data[i][j];
			}
		}

		return result;
	}

	@Override
	public IntMatrix subtract(IntMatrix subtrahend) {
		if (this.rows != subtrahend.rows || this.cols != subtrahend.cols) {
			throw new IllegalArgumentException("rows and cols must be equal when add");
		}

		IntMatrix result = new IntMatrix();

		result.rows = this.rows;
		result.cols = this.cols;

		result.data = new int[result.rows][result.cols];

		for (int i = 0; i < result.rows; i++) {
			for (int j = 0; j < result.cols; j++) {
				result.data[i][j] = this.data[i][j] - subtrahend.data[i][j];
			}
		}

		return result;
	}

	@Override
	public IntMatrix multiply(IntMatrix multiplicand) {
		return null;
	}

	@Override
	public Number determinant() {
		return null;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				sb.append(data[i][j]).append("\t");
			}
			sb.append("\n");
		}

		return sb.toString();
	}

	public static class Builder {
		public static IntMatrix of(List<List<Integer>> param) {
			if (param == null || param.size() == 0) {
				throw new IllegalArgumentException("param must have at least one bean");
			}

			IntMatrix intMatrix = new IntMatrix();
			intMatrix.rows = param.size();
			intMatrix.cols = param.get(0).size();
			intMatrix.data = new int[intMatrix.rows][intMatrix.cols];

			for (int i = 0; i < param.size(); i++) {
				List<Integer> oneRow = param.get(i);

				if (oneRow == null || oneRow.size() != intMatrix.cols) {
					throw new IllegalArgumentException("illegal");
				}

				for (int j = 0; j < oneRow.size(); j++) {
					intMatrix.data[i][j] = oneRow.get(j);
				}
			}

			return intMatrix;
		}
	}

}
